import { game, RenderRoot2D } from 'cc'
import { Singleton } from '../Base/Singleton'
import { DrawManager } from '../Scripts/UI/DrawManager'
import { createUINode } from '../utils'
import { FADE_DEFAULT_DURATION } from '../utils/constant'

export default class FaderManager extends Singleton {
  static get Instance() {
    return super.getInstance<FaderManager>()
  }

  private _fader:DrawManager = null

  get fader() {
    if(this._fader !== null) {
      return this._fader
    }

    const root = createUINode()
    root.addComponent(RenderRoot2D)
    const faderNode = createUINode()
    faderNode.setParent(root)
    this._fader = faderNode.addComponent(DrawManager)
    this._fader.init()
    // 持久化组件 只有RenderRoot2D组件和另外一种组件才能实现持久化
    game.addPersistRootNode(root)

    return this._fader
  }

  async fadeIn(duration: number = FADE_DEFAULT_DURATION) {
    await this.fader.fadeIn(duration)
  }

  async fadeOut(duration: number = FADE_DEFAULT_DURATION) {
    await this.fader.fadeOut(duration)
  }

  async mask() {
    await this.fader.mask()
  }


}
